#include <unistd.h>
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <sybfront.h>
#include <sybdb.h>

/* delete single quotes
 * process sql content
 */
static void sqlstrtrim(char *str)
{
    int length = 0;

    if (NULL == str)
        return;
    length = (int) strlen(str);
    for (; length >= 0; length--)
    {
        if (str[length] == 0x27 || str[length] == 0x22)
            str[length] = 0x20;
    }
    return;
}

/* set mssql connection
 * set connection attribute.
 */
void set_connect(DBPROCESS *mssql)
{
    dbcmd(mssql, "SET NUMERIC_ROUNDABORT OFF");
    dbsqlexec(mssql);

    dbcmd(mssql, "SET CONCAT_NULL_YIELDS_NULL ON");
    dbsqlexec(mssql);

    dbcmd(mssql, "SET ANSI_NULLS ON");
    dbsqlexec(mssql);

    dbcmd(mssql, "SET ANSI_PADDING ON");
    dbsqlexec(mssql);

    dbcmd(mssql, "SET ANSI_WARNINGS ON");
    dbsqlexec(mssql);

    dbcmd(mssql, "SET QUOTED_IDENTIFIER ON");
    dbsqlexec(mssql);

    dbcmd(mssql, "SET ARITHABORT ON");
    dbsqlexec(mssql);
    return;
}

/* Microsoft sql server
 * send warning to users. set for jd.com
 */
void sql_server(char *ip, const char *err_msg, int sql_errno)
{
    char *uname = "mysql_monitor_rw";
    char *upass = "xxxxxxxxxxxx";
    char *server = "xxxxxxxxxxxx";
    char sql[0x400];
    char sms[0xFF];

    fprintf(stdout, "(%d) %s\n", sql_errno, err_msg);
    sprintf(sms, "%s", err_msg);
    if (0x200 < strlen(err_msg))
        return;

    dbinit();
    LOGINREC *loginrec = dblogin();
    DBSETLUSER(loginrec, uname);
    DBSETLPWD(loginrec, upass);
    DBSETLCHARSET(loginrec, "UTF-8");

    DBPROCESS *mssql = dbopen(loginrec, server);
    if (mssql == FAIL)
    {
        fprintf(stderr, "Microsoft sql server connect failed.\n");
        return;
    }

    if (dbuse(mssql, "jd_msg") == FAIL)
    {
        fprintf(stderr, "Database select failed.\n");
        goto end;
    }

    sqlstrtrim(ip);
    sqlstrtrim(sms);

#if 1
    set_connect(mssql);
    sprintf(sql, "insert into jd_mobile_Msg(mobile_num, msg_content, is_success, sender_id) "
            "values('15210130499', '[分布式数据库]:%s, %s', 0, 1)", ip, sms);
    dbcmd(mssql, sql);

    if (dbsqlexec(mssql) == FAIL)
    {
        fprintf(stderr, "error:%s\n", sql);
        goto end;
    }

    sprintf(sql, "insert into jd_mobile_Msg(mobile_num, msg_content, is_success, sender_id) "
            "values('13911087826', '[分布式数据库]:%s, %s', 0, 1)", ip, sms);
    dbcmd(mssql, sql);

    if (dbsqlexec(mssql) == FAIL)
    {
        fprintf(stderr, "error:%s\n", sql);
        goto end;
    }

#else
    sprintf(sql, "select top 1 id from jd_mobile_Msg order by id desc");

    dbcmd(mssql, sql);
    if (dbsqlexec(mssql) == FAIL)
    {
        fprintf(stderr, "error:%s\n", sql);
        goto end;
    }
    DBINT result_code;
    char uid[0xFFF];
    int rows = 0;
    while ((result_code = dbresults(mssql)) != NO_MORE_RESULTS)
    {
        if (result_code == SUCCEED)
        {
            dbbind(mssql, 1, CHARBIND, (DBINT) 0, (BYTE*) uid);
            while (dbnextrow(mssql) != NO_MORE_ROWS)
            {
                rows++;
                fprintf(stdout, "%s\n", uid);
            }
        }
    }
    fprintf(stdout, "final row=%d\n", rows);
#endif

end:
    dbclose(mssql);
    return;
}

